package com.zzh.ideas.web.framework.handler;

import com.zzh.ideas.core.model.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * description: 对controller层返回值做统一处理，如分页查询对象统一封装，返回值类型统一转化等<br>
 * createTime: 2019/2/149:30 <br>
 *
 * @author zzh
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class ReturnValueHandler implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //TODO 待实现
        return true;
    }


    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if(!(body instanceof String) && !(body instanceof ResponseModel)) {
            return ResponseModel.success(body);
        }
        return body;
    }
}
